<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <!-- <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button> -->
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <!-- <j-upload-button type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button> -->
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
  </div>
</template>

<script lang="ts" name="order-order" setup>
import { ref, reactive, computed, unref, onMounted } from 'vue';
import { BasicTable, useTable, TableAction, BasicColumn } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { useListPage } from '/@/hooks/system/useListPage'
import { searchFormSchema, superQuerySchema } from './orderStatistics.data';
import { communityList, deleteOne, batchDelete, getImportUrl, getCommunityExportUrl, refund } from './orderStatistics.api';
import { useUserStore } from '/@/store/modules/user';
const queryParam = reactive<any>({});
const checkedKeys = ref<Array<string | number>>([]);
const userStore = useUserStore();
const dataSource = ref<any>([]);
async function getList() {
  const res = await communityList({});
  dataSource.value = res;
}
getList();
const columns: BasicColumn[] = [
  {
    title: '社区名称',
    align: 'center',
    dataIndex: 'community',
    customCell: (record, index) => {
      // 计算相同社区的行数
      let rowSpan = 1;
      for (let i = index + 1; i < dataSource.value.length; i++) {
        if (dataSource.value[i].community == record.community) {
          rowSpan++;
        } else {
          break;
        }
      }

      // 如果是合并行的非第一行，设置rowSpan为0
      if (index > 0 && dataSource.value[index - 1].community == record.community) {
        return { rowSpan: 0 };
      }
      // 合并行的第一行，设置rowSpan
      return { rowSpan };
    }
  },
  {
    title: '品类',
    align: 'center',
    dataIndex: 'category',
  },
  {
    title: '重量',
    align: 'center',
    dataIndex: 'weight',
  },
  {
    title: '规格',
    align: 'center',
    dataIndex: 'specification',
  },
];
//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '订单列表',
    dataSource,
    columns,
    canResize: false,
    formConfig: {
      //labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      showAdvancedButton: true,
      fieldMapToNumber: [
      ],
      fieldMapToTime: [
      ],
    },
    tableSetting: {
      redo: false
    },
    showActionColumn: false,
    actionColumn: {
      width: 120,
      fixed: 'right'
    },
    beforeFetch: (params) => {
      return Object.assign(params, queryParam);
    },
  },
  exportConfig: {
    name: "社区进货统计",
    url: getCommunityExportUrl,
    params: queryParam,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess
  },
})

const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext
/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
}
/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}
/**
   * 操作栏
   */
function getTableAction(record) {
  return [
    // {
    //   label: '编辑',
    //   onClick: handleEdit.bind(null, record)
    // },
  ]
}
</script>

<style lang="less" scoped>
:deep(.ant-picker),
:deep(.ant-input-number) {
  width: 100%;
}
</style>
